package com.ldj.struts.action;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import com.ldj.common.MenuBuilder;
import com.ldj.common.SystemConfig;
import com.ldj.common.SystemSession;
import com.ldj.hibernate.DataDictionary;
import com.ldj.hibernate.DataDictionaryDAO;
import com.ldj.hibernate.FunctionMenuDAO;
import com.ldj.hibernate.PersionFile;
import com.ldj.hibernate.PersionFileDAO;
import com.ldj.hibernate.SuperviseOrgan;
import com.ldj.hibernate.SuperviseOrganDAO;
import com.ldj.hibernate.SysLogInfo;
import com.ldj.hibernate.SysLogInfoDAO;
import com.ldj.hibernate.UserAreaScope;
import com.ldj.hibernate.UserManagerScope;
import com.ldj.struts.form.SuperviseOrganForm;

public class SuperviseOrganAction extends DispatchAction {

	public ActionForward list(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		SuperviseOrganForm sForm = (SuperviseOrganForm)form;
		SuperviseOrganDAO dao = new SuperviseOrganDAO(request, response);
		String code = request.getParameter("code");
		if(code !=null && !"".equals(code)){		
			if(code.equals("1")){
			   code = "";
		    }
			request.getSession().setAttribute("code", code);
		}
		code = (String)request.getSession().getAttribute("code");
		List<SuperviseOrgan> list= (List<SuperviseOrgan>)dao.findAllPage(code,sForm);
		request.setAttribute("intPage", String.valueOf(dao.intPage));
		request.setAttribute("list", list);
		return mapping.findForward("list");
	}
	
	public ActionForward detail(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		SuperviseOrganDAO dao = new SuperviseOrganDAO(request, response);
		SuperviseOrgan info = dao.findById(request.getParameter("sid"));
		request.setAttribute("sInfo", info);
		
		DataDictionaryDAO dao1 = new DataDictionaryDAO(request, response);
		DataDictionary dInfo = dao1.findFromCode(info.getSupOrganType());
		request.setAttribute("dInfo", dInfo);
		return mapping.findForward("detail");
	}
	
	public ActionForward toAdd(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		return mapping.findForward("toAdd");
	}
	
	public ActionForward toUpdate(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		SuperviseOrganDAO dao = new SuperviseOrganDAO(request, response);
		SuperviseOrgan info = dao.findById(request.getParameter("sid"));
		request.setAttribute("nowPage",request.getParameter("page"));
		request.setAttribute("sInfo", info);
		return mapping.findForward("toUpdate");
	}
	
	public ActionForward update(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		SuperviseOrganForm sForm = (SuperviseOrganForm)form;
		SuperviseOrganDAO dao = new SuperviseOrganDAO(request, response);
		SuperviseOrgan info = dao.findById(request.getParameter("sid"));
		info.setSupOrganName(sForm.getSupOrganName());
		info.setSupOrganAdministrativeLevel(sForm.getSupOrganAdministrativeLevel());
		info.setSupOrganAddress(sForm.getSupOrganAddress());
		info.setSupOrganTelNum(sForm.getSupOrganTelNum());
		info.setSupOrganYOrgan(sForm.getSupOrganYOrgan());
		info.setSupOrganHOrgan(sForm.getSupOrganHOrgan());
		info.setSupOrganZOrgan(sForm.getSupOrganZOrgan());
		info.setSupOrganXPerson(sForm.getSupOrganXPerson());
		info.setSupOrganJPerson(sForm.getSupOrganJPerson());
		info.setSupOrganZPerson(sForm.getSupOrganZPerson());
		dao.merge(info);
		
		SysLogInfoDAO logDao = new SysLogInfoDAO(null, null);
		SysLogInfo s = new SysLogInfo();
		s.setPersionFile(SystemSession.getSupUserInfo(request));
		s.setSysLogInfoOperDate(new Date());
		s.setSysLogInfoOperType("1");
		s.setSysLogInfoSupOrganCode(SystemSession.getSupUserInfo(request).getPerFileOrganCode());
		s.setSysLogInfoOperRecord(SystemConfig.getString("syslog", "SuperviseOrgan.update").replace("$user", SystemSession.getSupUserInfo(request).getPerFileName()).replace("$record", info.getSupOrganName()));
		logDao.save(s);
		return new ActionForward("/SuperviseOrgan.do?method=list&pages="+request.getParameter("pages"), true);
	}
	
	public ActionForward add(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		SuperviseOrganForm sForm = (SuperviseOrganForm)form;
		SuperviseOrgan info = new SuperviseOrgan();

		SuperviseOrganDAO dao = new SuperviseOrganDAO(request, response);
		
		String code = (String)request.getSession().getAttribute("code");
		
		List<SuperviseOrgan> list= (List<SuperviseOrgan>)dao.getSonOrganFromCode(code);
		
		String codeMax = code+"001";
		
		if(list !=null &&list.size()>0){
		  codeMax = ((SuperviseOrgan)list.get(list.size()-1)).getSupOrganCode();
		  int intcodemax = Integer.parseInt(codeMax.substring(codeMax.length() - 3))+1;
		  if((""+intcodemax).length()%3==1){
			  codeMax = "00"+intcodemax;			  
		  }else if((""+intcodemax).length()%3==2){
			  codeMax = "0"+intcodemax;
		  }
		  codeMax = code + codeMax;
		}
		info.setSupOrganCode(codeMax);
		info.setSupOrganName(sForm.getSupOrganName());
		info.setSupOrganAdministrativeLevel(sForm.getSupOrganAdministrativeLevel());
		info.setSupOrganAddress(sForm.getSupOrganAddress());
		info.setSupOrganTelNum(sForm.getSupOrganTelNum());
		
		info.setSupOrganYOrgan(sForm.getSupOrganYOrgan());
		info.setSupOrganHOrgan(sForm.getSupOrganHOrgan());
		info.setSupOrganZOrgan(sForm.getSupOrganZOrgan());
		info.setSupOrganXPerson(sForm.getSupOrganXPerson());
		info.setSupOrganJPerson(sForm.getSupOrganJPerson());
		info.setSupOrganZPerson(sForm.getSupOrganZPerson());
		info.setSupOrganDelOper("0");
		dao.save(info);
		
		SysLogInfoDAO logDao = new SysLogInfoDAO(null, null);
		SysLogInfo s = new SysLogInfo();
		s.setPersionFile(SystemSession.getSupUserInfo(request));
		s.setSysLogInfoOperDate(new Date());
		s.setSysLogInfoOperType("0");
		s.setSysLogInfoSupOrganCode(SystemSession.getSupUserInfo(request).getPerFileOrganCode());
		s.setSysLogInfoOperRecord(SystemConfig.getString("syslog", "SuperviseOrgan.add").replace("$user", SystemSession.getSupUserInfo(request).getPerFileName()).replace("$record", info.getSupOrganName()));
		logDao.save(s);
		return new ActionForward("/SuperviseOrgan.do?method=list", true);
	}
	
	public ActionForward delete(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		SuperviseOrganDAO dao = new SuperviseOrganDAO(request, response);
		SuperviseOrgan info = dao.findById(request.getParameter("sid"));
		List list = dao.findByCode(info.getSupOrganCode());
		
		if(list != null && list.size() > 0){
			for(int i = 0; i < list.size(); i ++){
				SuperviseOrgan info1 =(SuperviseOrgan)list.get(i);
				info1.setSupOrganDelOper("1");
				dao.merge(info1);
			}
		}
		
		SysLogInfoDAO logDao = new SysLogInfoDAO(null, null);
		SysLogInfo s = new SysLogInfo();
		s.setPersionFile(SystemSession.getSupUserInfo(request));
		s.setSysLogInfoOperDate(new Date());
		s.setSysLogInfoOperType("2");
		s.setSysLogInfoSupOrganCode(SystemSession.getSupUserInfo(request).getPerFileOrganCode());
		s.setSysLogInfoOperRecord(SystemConfig.getString("syslog", "SuperviseOrgan.delete").replace("$user", SystemSession.getSupUserInfo(request).getPerFileName()).replace("$record", info.getSupOrganName()));
		logDao.save(s);
		
		return new ActionForward("/SuperviseOrgan.do?method=list&pages="+request.getParameter("page"), true);
	}
	
	public ActionForward tree(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
	//	String code = request.getParameter("code");
		String value = "";
		PersionFileDAO dao = new PersionFileDAO(request,response);
		List<UserManagerScope> infoList = dao.findBySuperviseOrganId(SystemSession.getSupUserId(request));
		if(infoList != null && infoList.size()>0){
			UserManagerScope info1 =infoList.get(0);
			value = info1.getUserManagerScopeOrganId();
		}
		MenuBuilder bu = new MenuBuilder();
		request.setAttribute("tree", bu.getPowerSuperviseOrganTree(request, response, "监察机构", "", "SuperviseOrgan.do?method=list&code=$code", value));		
		return mapping.findForward("tree");
	}
	
	public ActionForward toCheckScore(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		SuperviseOrganDAO dao = new SuperviseOrganDAO(request, response);
		SuperviseOrgan info = dao.findById(request.getParameter("sid"));
		request.setAttribute("sInfo", info);
		return mapping.findForward("toCheckScore");
	}
	
	public ActionForward checkScore(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		SuperviseOrganForm sForm = (SuperviseOrganForm)form;
		SuperviseOrganDAO dao = new SuperviseOrganDAO(request, response);
		SuperviseOrgan info = dao.findById(request.getParameter("sid"));
		PersionFile pInfo =(PersionFile) request.getSession().getAttribute("UserInfo");
		info.setSupOrganCheckScore(sForm.getSupOrganCheckScore());
		info.setSupOrganCheckUserId(pInfo.getPerFileId());
		info.setSupOrganCheckDate(new Date());
		dao.merge(info);
		return new ActionForward("/SuperviseOrgan.do?method=list", true);
	}
}